'''xpath_案例4爬取3gbizhi网站壁纸

https://www.3gbizhi.com/
页面整体非常清爽简洁，图片质量也可以自定义筛选，整体风格走炫酷风，下方也有许多标签可供分类筛选，
宅男首选。优点是图片清晰度高，搜索功能强，可以推荐与关键字相关的图片。网站除了电脑壁纸，还有大量的手机壁纸。

爬取壁纸类型 分页示例：
    https://www.3gbizhi.com/meinv
    https://www.3gbizhi.com/meinv/index_2.html
    https://www.3gbizhi.com/meinv/index_3.html
    https://www.3gbizhi.com/meinv/index_4.html
    ....
'''
# 引入网络请求包
import urllib.request
# 引入xpath解析网页包
from lxml import etree
# 引入操作文件包
import os


# 构建一个Request
def build_request(page):
    threegbizhi_url = 'https://www.3gbizhi.com/meinv'
    # 三目运算符 判断 page 等于1 使用默认地址 否则 默认地址拼接分页值；
    threegbizhi_url = threegbizhi_url if page == 1 else threegbizhi_url+'/index_'+str(page)+'.html'
    threegbizhi_headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
    }
    # print(threegbizhi_url)
    request = urllib.request.Request(url=threegbizhi_url, headers=threegbizhi_headers)
    return request


# 获取请求返回体
def get_result(request):
    response = urllib.request.urlopen(request)
    result = response.read().decode('utf-8')
    return result


# 下载图片到本地
def down_img(page, img_name_list, img_url_list):
    local_path = 'D:\\hxy\\test\\爬取3gbizhi网站壁纸'
    local_path = local_path + '\\'+ str(page)
    existFlag = os.path.exists(local_path)
    if existFlag == False:
        # 文件不存在，创建
        os.mkdir(local_path)

    for index in range(len(img_name_list)):
        img_url = img_url_list[index]
        img_name = img_name_list[index]
        print(img_name, img_url)
        save_path = local_path +'\\'+img_name+'.jpg'
        # 下载网络图片 https://pic.3gbizhi.com/uploads/20220716/f4831b1c91556fc8a04163ccdd0353f0.jpg
        urllib.request.urlretrieve(img_url, save_path)


if __name__ == '__main__':
    start_num = int(input('请输入起始页：'))
    end_num = int(input('请输入终止页：'))
    # 三目运算符 处理起始页小于1 默认为1
    start_num = 1 if start_num <= 0 else start_num
    # 循环获取分页 页数
    for page in range(start_num, end_num+1):
        # print(page)
        request = build_request(page)
        result = get_result(request)
        html = etree.HTML(result)
        img_name_list = html.xpath('//ul/li/a[@class="imgw"]/img/@alt')
        img_url_list = html.xpath('//ul/li/a[@class="imgw"]/img/@lay-src')
        down_img(page, img_name_list, img_url_list)







'''
#初始示例
threegbizhi_url = 'https://www.3gbizhi.com/meinv'
threegbizhi_headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}
request = urllib.request.Request(url=threegbizhi_url, headers=threegbizhi_headers)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
print(content)

html = etree.HTML(content)
img_name_list = html.xpath('//ul/li/a[@class="imgw"]/img/@alt')
img_url_list = html.xpath('//ul/li/a[@class="imgw"]/img/@lay-src')
print(len(img_name_list), img_name_list)
print(len(img_url_list), img_url_list)
for index in range(len(img_name_list)):
    print(img_name_list[index], img_url_list[index])
'''



